package com.tte.common.designpattern.strategy;

/**
 * 使用 Context 来查看当它改变策略 Strategy 时的行为变化
 */
public class StrategyPatternDemo {
    public static void main(String[] args) {
        ContextT context = new ContextT(new OperationAddT());
        System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

        context = new ContextT(new OperationSubtractT());
        System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

        context = new ContextT(new OperationMultiplyT());
        System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
    }
}
//接口进行算数
interface StrategyT {
    public int doOperation(int num1, int num2);
}
// 加法算数
class OperationAddT implements StrategyT{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}
//减法算数
class OperationSubtractT implements StrategyT{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}
//乘法算数
class OperationMultiplyT implements StrategyT{
    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }
}

class ContextT {
    private StrategyT strategy;

    public ContextT(StrategyT strategy){
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2){
        return strategy.doOperation(num1, num2);
    }
}